import {createApp} from 'vue'
import App from './App.vue'
import routes from "@/routes";
import 'normalize.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 记录重连次数
let reconnectCount = 0;

const app = createApp(App)
// 创建WebSocket连接
createWebSocket(8888);

app.use(routes)
app.use(ElementPlus)
app.mount('#app')


function createWebSocket(port) {

    // 创建WebSocket对象
    let ws = new WebSocket('ws://localhost:' + port);
    window.ws=ws
    // 当连接建立时调用
    ws.onopen = function () {
        console.log('WebSocket连接已建立');
        // 重置重连计数器
        reconnectCount = 0;
    };

    // 当连接关闭时调用
    ws.onclose = function () {
        console.log('WebSocket连接已关闭');

        // 如果重连次数不超过3次，则尝试重新连接
        if (reconnectCount < 5) {
            reconnectCount++;
            console.log(`正在尝试第 ${reconnectCount} 次重新连接...`);
            setTimeout(() => {
                createWebSocket(8888);
            }, 3000); // 延迟3秒后重新连接
        } else {
            console.log('重连失败，已达到最大重连次数');
        }
    };
}


